// Created by Joe on 2017/6/8.

/**
 * @type:
 *      0: yyyymmdd -> yyyy/mm/dd
 *      1: yyyy-mm-dd hh:mm:ss -> yyyy年mm月dd日 hh:mm:ss
 *      2: yyyy-mm-dd hh:mm:ss -> mm/dd hh
 *      3: yyyymm -> yyyy年mm月
 *      4: total-second -> yyyy-mm-dd hh:mm:ss
 *      5: total-second -> yyyymm/yyyymmdd/mmdd (customize)
 */

app.filter(
    'mul_date',
    function () {
        return function (time, type) {
            function padding(val, limit) {
                limit = limit ? +limit : 10;
                return val < limit ? '0' + val : val;
            }

            if (!time) return '';

            var YMD, HMS;
            var date, year, month, day, hour, minute, second;

            if (type === 0) {
                return [time.substr(0, 4), time.substr(4, 2), time.substr(6, 2)].join('/');
            }

            if (type === 1) {
                YMD = time.split(' ')[0].split('-');
                HMS = time.split(' ')[1];
                return YMD[0] + '年' + YMD[1] + '月' + YMD[2] + '日 ' + HMS;
            }

            if (type === 2) {
                YMD = time.split(' ')[0].split('-');
                HMS = time.split(' ')[1].split(':');
                return YMD.slice(1, 3).join('/') + ' ' + HMS[0];
            }

            if (type === 3) {
                return time.substr(0, 4) + '年' + time.substr(4) + '月';
            }

            // Date.getLocaleString => not padding, such as 2017-5-18 15:15:22
            if (type === 4) {
                date = new Date(time);
                year = date.getFullYear();
                month = padding(date.getMonth() + 1);
                day = padding(date.getDate());
                hour = padding(date.getHours());
                minute = padding(date.getMinutes());
                second = padding(date.getSeconds());
                return [year, month, day].join('-') + ' ' + [hour, minute, second].join(':');
            }

            if (type === 5) {
                date = new Date(time);
                year = date.getFullYear();
                month = padding(date.getMonth() + 1);
                return [year, month].join('');
            }

            return time;
        };
    }
);